;; AutoComplete 

(use-package company
  :init (add-hook 'after-init-hook 'global-company-mode)
  :config
  (defun check-expansion ()
    (save-excursion
      (if (looking-at "\\_>") t
	(backward-char 1)
	(if (looking-at "\\.") t
	  (backward-char 1)
	  (if (looking-at "->") t nil)))))

  (defun do-yas-expand ()
    (let ((yas/fallback-behavior 'return-nil))
      (yas/expand)))

  (defun tab-indent-or-complete ()
    (interactive)
    (if (minibufferp)
	(minibuffer-complete)
      (if (or (not yas/minor-mode)
	      (null (do-yas-expand)))
	  (if (check-expansion)
	      (company-complete-common)
	    (indent-for-tab-command)))))
  (global-set-key [tab] 'tab-indent-or-complete)

  (bind-keys :map company-mode-map
			 ("<tab>" . nil)
			 ("C-i" . company-complete))
  (bind-keys :map company-active-map
			 ("C-j" . company-select-next)
			 ("C-k" . company-select-previous)
			 ("C-s" . company-search-words-regexp))
  (bind-keys :map company-search-map
			 ("C-j" . company-select-next)
			 ("C-p" . company-select-previous))
  (define-key company-active-map [tab] 'company-complete-common-or-cycle)
  (define-key company-active-map (kbd "TAB") 'company-complete-common-or-cycle)

  (setq company-idle-delay 0.0
	company-echo-delay 0.0
	company-minimum-prefix-length 1
	company-selection-wrap-around t
	company-show-numbers nil
	company-tooltip-limit 20
	company-tooltip-align-annotations t
	company-dabbrev-ignore-case t
	company-dabbrev-downcase nil)
  )

(provide 'init-company)